package i.i.myapplication;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import i.i.myapplication.controller.WeChatAdapter;
import okhttp3.OkHttpClient;

public class WeChatFragment extends Fragment {
    private static final int REQUEST_CODE_ADD = 1001;

    private RecyclerView recyclerView;
    private SwipeRefreshLayout swipeRefreshLayout;
    private Button btnAdd;

    private List<Integer> imageResIdList;
    private List<String> titleList;
    private List<String> descriptionList;

    private WeChatAdapter adapter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_wechat, container, false);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        swipeRefreshLayout = view.findViewById(R.id.swipeRefreshLayout);
        recyclerView = view.findViewById(R.id.rv_wechat);
        btnAdd = view.findViewById(R.id.btn_add);

        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

        // 初始化数据用ArrayList，方便增删
        imageResIdList = new ArrayList<>(Arrays.asList(
                R.drawable.img_01,
                R.drawable.img_02,
                R.drawable.img_03
        ));
        titleList = new ArrayList<>(Arrays.asList(
                "艾薇儿",
                "薛之谦",
                "jy"
        ));
        descriptionList = new ArrayList<>(Arrays.asList(
                "描述文字1",
                "描述文字2",
                "描述文字3"
        ));

        adapter = new WeChatAdapter(getContext(),
                imageResIdList,
                titleList,
                descriptionList);

        recyclerView.setAdapter(adapter);

        btnAdd.setOnClickListener(v -> {
            // 跳转到详情页
            Intent intent = new Intent(getContext(), WeChatDetailActivity.class);
            startActivityForResult(intent, REQUEST_CODE_ADD);
        });

        swipeRefreshLayout.setOnRefreshListener(() -> {
            // 模拟刷新
            new Handler().postDelayed(() -> {
                swipeRefreshLayout.setRefreshing(false);
                Toast toast = Toast.makeText(getContext(), "刷新完毕", Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.show();
            }, 2000);
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_ADD && resultCode == Activity.RESULT_OK && data != null) {
            String title = data.getStringExtra("title");
            String desc = data.getStringExtra("description");
            int imgResId = data.getIntExtra("imageResId", R.drawable.default_news_img);

            // 添加新数据并刷新
            imageResIdList.add(imgResId);
            titleList.add(title);
            descriptionList.add(desc);

            adapter.notifyDataSetChanged();
            recyclerView.scrollToPosition(imageResIdList.size() - 1);
        }
    }
}
